﻿
package com.android.finaltracer.model;

import org.json.JSONException;
import org.json.JSONObject;

public class UserBindInfo extends DataFormatBasic {
    public final static int USERBINDINFO_NO_COMMAND = 0;
    // public final static int
    // UI_SERVICE_BIND_PHONE_REQ=USERBINDINFO_NO_COMMAND+1;
    public final static int REMOTE_BIND_PHONE_REQ = USERBINDINFO_NO_COMMAND + 1;
    // public final static int
    // UI_SERVICE_BIND_PHONE_CNF=REMOTE_BIND_PHONE_REQ+1;
    public final static int REMOTE_BIND_PHONE_CNF = REMOTE_BIND_PHONE_REQ + 1;
    // public final static int
    // UI_SERVICE_BIND_PHONE_RSP=REMOTE_BIND_PHONE_CNF+1;
    public final static int REMOTE_BIND_PHONE_RSP = REMOTE_BIND_PHONE_CNF + 1;
    private static final String TAG_COMMAND = "command";
    private static final String TAG_PHONE_NUM = "phonenum";
    private static final String TAG_IMEI = "imei";
    private static final String TAG_ACCEPT = "accept";
    private static final String TAG_IS_REQUEST_BIND = "isRequestBind";
    private int command;
    // 接收方的号码
    private String toPhoneNum;
    // 自己的imei
    private String imei;
    private boolean accept;
    private boolean isRequestBind;

    public UserBindInfo() {
        super();
    }

    public UserBindInfo(int command, String phoneNum, String imei,
            boolean accept, boolean isRequestBind) {
        super();
        this.command = command;
        this.toPhoneNum = phoneNum;
        this.imei = imei;
        this.accept = accept;
        this.isRequestBind = isRequestBind;
    }

    public UserBindInfo(int command, String phoneNum, String imei,
            boolean accept) {
        this(command, phoneNum, imei, accept, false);
    }

    public UserBindInfo(int command, String phoneNum, String imei) {
        this(command, phoneNum, imei, false, false);
    }

    public int getCommand() {
        return command;
    }

    public void setCommand(int command) {
        this.command = command;
    }

    public String getToPhoneNum() {
        return toPhoneNum;
    }

    public void setToPhoneNum(String toPhoneNum) {
        this.toPhoneNum = toPhoneNum;
    }

    public String getImei() {
        return imei;
    }

    public void setImei(String imei) {
        this.imei = imei;
    }

    public boolean isAccept() {
        return accept;
    }

    public void setAccept(boolean accept) {
        this.accept = accept;
    }

    public boolean isRequestBind() {
        return isRequestBind;
    }

    public void setRequestBind(boolean isRequestBind) {
        this.isRequestBind = isRequestBind;
    }

    public String encode() {
        String codeStr = "";
        try {
            JSONObject codeJson = new JSONObject(makeHeader());
            codeJson.put(
                    TAG_BODY,
                    new JSONObject()
                            .put(TAG_COMMAND, command)
                            .put(TAG_PHONE_NUM,
                                    toPhoneNum != null ? toPhoneNum : null)
                            .put(TAG_IMEI, imei != null ? imei : null)
                            .put(TAG_ACCEPT, accept ? accept : null)
                            .put(TAG_IS_REQUEST_BIND,
                                    isRequestBind ? isRequestBind : null));
            codeStr += codeJson.toString();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return codeStr;
    }

    public boolean decode(String str) {
        // mCommand = Integer.parseInt(str);
        try {
            JSONObject json = new JSONObject(str);
            command = json.getInt(TAG_COMMAND);
            if (json.has(TAG_PHONE_NUM)) {
                toPhoneNum = json.getString(TAG_PHONE_NUM);
            }
            if (json.has(TAG_IMEI)) {
                imei = json.getString(TAG_IMEI);
            }
            if (json.has(TAG_ACCEPT)) {
                accept = json.getBoolean(TAG_ACCEPT);
            }
            if (json.has(TAG_IS_REQUEST_BIND)) {
                isRequestBind = json.getBoolean(TAG_IS_REQUEST_BIND);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public int getType() {
        // TODO Auto-generated method stub
        return DataFormatBasic.TYPE_BIND_INFO;
    }
}
